package servlet;

import dao.UserDao;
import model.Order;
import util.JDBCUtil; // 你自己的数据库工具类

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/SetOrder.do")
public class SetOrder extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 获取订单信息
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(username);
        // 使用UserDao根据username获取user_id
        UserDao userDao = new UserDao();
        int user_id = userDao.getUserIdByUsername(username);

        if (username == null) {
            // 如果用户未登录或者 user_id 不是 Integer 类型，则重定向到登录页面或者其他处理方式
            response.sendRedirect(request.getContextPath() + "/home/login-2.jsp");
            return;
        }
        Order order=new Order();

        int order_id = order.generateOrderID();
        System.out.println(order_id);
        String recipient_name = request.getParameter("recipient_name");
        String recipient_address = request.getParameter("recipient_address");
        String recipient_phone = request.getParameter("recipient_phone");
        String notes = request.getParameter("notes");

        // 创建 Order 对象并设置属性
     order = new Order();
        order.setUser_id((user_id));
        order.setOrder_id(order_id);
        order.setRecipient_name(recipient_name);
        order.setRecipient_address(recipient_address);
        order.setRecipient_phone(recipient_phone);
        order.setNotes(notes);
        System.out.println(order.getUser_id()+" "+ order.getRecipient_name()+" "+order.getRecipient_address()+" "+order.getRecipient_phone()+" "+order.getNotes());
        response.sendRedirect(request.getContextPath() + "/Front/checkout.jsp");
        // 可选步骤：保存订单到数据库
//         boolean isSaved = saveOrderToDatabase(order);
//        if (isSaved) {
//
//        } else {
//            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Order not saved");
//        }
        // 可以根据需要重定向到订单详情页面或者其他页面

    }

    private Boolean saveOrderToDatabase(Order order) {
        JDBCUtil jdbcUtil = new JDBCUtil(); // 假设有一个数据库工具类
        String sql = "INSERT INTO `order` (order_id, user_id, recipient_name, recipient_address, recipient_phone, notes) VALUES (?, ?, ?, ?, ?, ?)";
        Boolean A=false;

        try {
                A= jdbcUtil.paramsUpdate(sql, order.getOrder_id(), order.getUser_id(), order.getRecipient_name(),
                    order.getRecipient_address(), order.getRecipient_phone(), order.getNotes());
        } finally {
            jdbcUtil.closeConnection();
        }
            return  A;
    }
}
